package com.david.rabbitmq.spring.annotation.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author David
 * @version V1.0
 * @Package com.david.rabbitmq.spring.annotation.config
 * @date 2021/7/17 14:48
 */
@Configuration
public class RabbitMqConfig {
    /**
     * rabbitmq的ConnectionFactory
     * @return
     */
    @Bean
    public com.rabbitmq.client.ConnectionFactory  getConnectionFactory(){
        com.rabbitmq.client.ConnectionFactory  factory = new com.rabbitmq.client.ConnectionFactory ();
        //配置连接信息
        factory.setHost("192.168.219.203");
        factory.setVirtualHost("/");
        factory.setUsername("root");
        factory.setPassword("123456");
        factory.setPort(5672);
        return factory;
    }

    /**
     * spring提供的connectionFactory
     * @param rabbitFactory
     * @return
     */
    @Bean
    public ConnectionFactory connectionFactory(com.rabbitmq.client.ConnectionFactory rabbitFactory) {
        ConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitFactory);
        return connectionFactory;
    }

    /**
     * AmqpAdmin
     * @param factory
     * @return
     */
    @Bean
    public AmqpAdmin amqpAdmin(ConnectionFactory factory) {
        AmqpAdmin amqpAdmin = new RabbitAdmin(factory);
        return amqpAdmin;
    }

    /**
     * RabbitTemplate
     * @param factory
     * @return
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory factory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        return rabbitTemplate;
    }

    /**
     * 队列，可以配置多个队列，主要修改beanname属性，注入的时候使用@Qualifier注解指定特定的bean
     * @return
     */
    @Bean(name = "q1")
    public Queue queue1() {
        Queue queue = new Queue("annotation.q1");
        return queue;
    }
    @Bean(name = "q2")
    public Queue queue2() {
        Queue queue = new Queue("annotation.q2");
        return queue;
    }
    @Bean(name = "q3")
    public Queue queue3() {
        Queue queue = new Queue("annotation.q3");
        return queue;
    }

    /**
     * FanoutExchangeExchange
     * @return
     */
    @Bean(name = "fanout")
    public Exchange fanoutExchange() {
        return new FanoutExchange("ex.anno.fanout", false, false, null);
    }

    /**
     * DirectExchange
     * @return
     */
    @Bean(name = "direct")
    public Exchange directExchange() {
        return new DirectExchange("ex.anno.direct", false, false, null);
    }

    /**
     * TopicExchange
     * @return
     */
    @Bean(name = "topic")
    public Exchange topicExchange() {
        return new TopicExchange("ex.anno.topic", false, false, null);
    }

    /**
     * fanout 绑定q1
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    @Autowired
    public Binding fanoutBinding(@Qualifier("q1") Queue queue, @Qualifier("fanout") Exchange exchange) {
        // 创建一个绑定，不指定绑定的参数
        final Binding binding = BindingBuilder.bind(queue).to(exchange).with("").noargs();
        return binding;
    }

    /**
     * direct 绑定q2
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    @Autowired
    public Binding directBinding(@Qualifier("q2") Queue queue, @Qualifier("direct") Exchange exchange) {
        // 创建一个绑定，不指定绑定的参数
        final Binding binding = BindingBuilder.bind(queue).to(exchange).with("key.anno.direct").noargs();
        return binding;
    }

    /**
     * topic 绑定q3
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    @Autowired
    public Binding topicBinding(@Qualifier("q3") Queue queue, @Qualifier("topic") Exchange exchange) {
        // 创建一个绑定，不指定绑定的参数
        final Binding binding = BindingBuilder.bind(queue).to(exchange).with("#.topic").noargs();
        return binding;
    }
}
